﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Carbon_Tracker
{
    public partial class frmDispatch : Form
    {
        private TimeSpan grouping = new TimeSpan(1, 0, 0, 0);
        public frmDispatch()
        {
            InitializeComponent();
            DatabaseInterface.Connection cnx = new DatabaseInterface.Connection();
            BaseUnits.Builders.PowerPlantBuilder pbuilder = new BaseUnits.Builders.PowerPlantBuilder(cnx);
            foreach (BaseUnits.IPowerPlant pl in pbuilder.getPowerPlants())
            {
                this.chkListPlants.Items.Add(pl);
            }
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                this.gridDispatch.DataSource = null;
                Cursor.Current = Cursors.WaitCursor;
                DateTime fromDate = dtpFrom.Value;
                DateTime toDate = dtpTo.Value;
                fromDate = fromDate.Date;
                toDate = toDate.Date;
                toDate = toDate.AddHours(25);
                System.Collections.Generic.List<BaseUnits.IPowerPlant> plantList = new System.Collections.Generic.List<BaseUnits.IPowerPlant>();

                foreach (BaseUnits.IPowerPlant p in this.chkListPlants.CheckedItems)
                {
                    plantList.Add(p);
                }
                Formulas.DispatchReport report = new Formulas.DispatchReport();

                DataTable table = report.GetDispatchData(fromDate, toDate, plantList, grouping);
                
                this.gridDispatch.DataSource = table;
                this.gridDispatch.Refresh();
                
                chart.Series.Clear();
                foreach (DataColumn c in table.Columns)
                {
                    if ((c.ColumnName != "Date") && (c.ColumnName != "Total") && (c.ColumnName != "Hour"))
                    {
                        chart.Series.Add(c.ColumnName);
                        chart.Series[c.ColumnName].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedArea;
                        chart.Series[c.ColumnName].XValueMember = "Date";
                        chart.Series[c.ColumnName].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
                        chart.Series[c.ColumnName].YValueMembers = c.ColumnName;
                        chart.Series[c.ColumnName].ToolTip = chart.Series[c.ColumnName].YValueMembers.ToString() + "\n #VALX{d}\n #VALY{d}";
                        ToolStripMenuItem tItem = new ToolStripMenuItem(c.ColumnName);
                        tItem.CheckOnClick = true;
                        tItem.Checked = true;
                        tItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
                        this.contextMenuPlants.Items.Add(tItem);
                    }
                }
                chart.DataSource = table;
                chart.DataBind();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetGrouping();
        }
        private void SetGrouping()
        {
            string group = cmbGrouping.SelectedItem.ToString() ;
            switch (group)
            {
                case "Hour":
                    this.grouping = new TimeSpan(0, 1, 0, 0);
                    break;
                case "Day":
                    this.grouping = new TimeSpan(1, 0, 0, 0);
                    break;
                case "Week": 
                    this.grouping = new TimeSpan(7, 0, 0, 0);
                    break;
                case "Month": 
                    this.grouping = new TimeSpan(30, 0, 0, 0);
                    break;
                
            }

        }

        private void frmDispatch_Load(object sender, EventArgs e)
        {

        }

        private void chart_MouseHover(object sender, EventArgs e)
        {
            
        }

        private void contextMenuPlants_Opening(object sender, CancelEventArgs e)
        {

        }

        private void contextMenuPlants_MouseClick(object sender, MouseEventArgs e)
        {
            
        }

        private void contextMenuPlants_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            
        }

        private void contextMenuPlants_MouseUp(object sender, MouseEventArgs e)
        {
            
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            foreach (ToolStripMenuItem t in this.contextMenuPlants.Items)
            {
                this.chart.Series[t.Text].Enabled = t.Checked;
            }
            this.chart.Update();
            this.Cursor = Cursors.Default;
        }
    }
}
